package marchsoft.modules.selfFish.pisciculture.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import marchsoft.exception.BadRequestException;
import marchsoft.modules.selfFish.pisciculture.entity.dto.FishDto;
import marchsoft.modules.selfFish.pisciculture.service.FishService;
import marchsoft.response.Result;
import marchsoft.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**鱼群管理
 * @title FishController
 * @description 鱼群管理
 * @author Lenovo
 * @version 1.0
 * @create 2024-07-15 16:53
 */
@Slf4j
@RestController
@RequestMapping("/api/fish")
@Api(tags = "养鱼管理：鱼群管理")
public class FishController {

    @Autowired
    private FishService fishService;

    private static final String ENTITY_NAME = "fish";

    @GetMapping
    @ApiOperation("获取鱼群列表")
    @PreAuthorize("@smpe.check('fish:list')")
    public Result<Object> query(String fishName,
                                @RequestParam(value ="pageNum",defaultValue = "1")Long pageNum,
                                @RequestParam(value ="pageSize",defaultValue = "10") Long pageSize ){
        log.info(StrUtil.format("【获取鱼群列表 /api/fish】操作人id：{}，鱼群查询条件 fishName：{}，分页pageNum：{}", SecurityUtils.getCurrentUserId(),fishName, pageNum));
        return fishService.getFishList(fishName,pageNum,pageSize);
    }

    @PostMapping
    @ApiOperation("新增鱼群")
    @PreAuthorize("@smpe.check('fish:add')")
    public Result<Object> create(@RequestBody FishDto fishDto){
        log.info(StrUtil.format("【新增鱼群 /api/fish】操作人id：{}，新增鱼群实体：{}", SecurityUtils.getCurrentUserId(), fishDto));
        if (ObjectUtil.isNotNull(fishDto.getId())) {
            log.error(StrUtil.format("【新增鱼群失败】操作人id：{}，新增鱼群实体默认id应该为空，dept：{}", SecurityUtils.getCurrentUserId(), fishDto));
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        return fishService.create(fishDto);
    }

    @DeleteMapping
    @ApiOperation("删除鱼群")
    @PreAuthorize("@smpe.check('fish:del')")
    public Result<Object> delete(@RequestBody Set<Long> ids){
        log.info(StrUtil.format("【删除鱼群 /api/fish】操作人id：{}，被删除鱼群id集：{}", SecurityUtils.getCurrentUserId(), ids.toString()));
        return fishService.delete(ids);
    }

    @PutMapping
    @ApiOperation("修改鱼群")
    @PreAuthorize("@smpe.check('fish:edit')")
    public Result<Object> update(@RequestBody FishDto fishDto){
        log.info(StrUtil.format("【修改鱼群 /api/fish】操作人id：{}，被修改鱼群id：{}", SecurityUtils.getCurrentUserId(), fishDto.getId()));
        return fishService.update(fishDto);
    }

    @ApiOperation("导出鱼群数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@smpe.check('fish:list')")
    public void download(HttpServletResponse response, String fishName) throws IOException {
        log.info(StrUtil.format("【查询鱼群 /api/fish】操作人id：{}，鱼群查询条件 fishName：{}", SecurityUtils.getCurrentUserId(), fishName));
        fishService.download(fishName,response);
    }
}
